﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace MaxLeaf.DAL.W2K
{
   


    public class BaseGroupDAL : BaseDAL
    {
        
        protected string strLogType = null;

        public BaseGroupDAL()
            : base()
        {
            
        }


        public override int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {


            string strSql = "select K3KeyValue from mWebK3KeyMap  where TableName=@TableName and WebKeyValue=@WebKeyValue";
            string strFitemId = dbDestK3Con.GetValue(strSql, new SqlParameter[]{
                new SqlParameter("@TableName",strWebMapKeyTableName)
                ,new SqlParameter("@WebKeyValue",strKeyValue)
            });
            
             
                DataRow drDest = dbDestK3Con.GetDataFirstRow("select top 1 FitemID from  t_item  where FitemID=@FitemID and FItemClassID=@FItemClassID", new SqlParameter[] { new SqlParameter("@FitemID", strFitemId), new SqlParameter("@FItemClassID", strFItemClassID) });
                if (drDest == null)
                {
                    strFitemId = string.Empty;
                }
             
             
                string[] arrFnumber, arrFName, arrKeyValue;
                GetFNumber(dbOrgWebReadCon, dbOrgWebWriteCon,dbDestK3Con, strKeyValue, out arrFnumber, out arrFName, out arrKeyValue);
                if (strFitemId.Length > 0)
                {
                    string strOldFNumber = dbDestK3Con.GetValue("select fnumber from t_item   where FitemID=@FitemID and FItemClassID=@FItemClassID", new SqlParameter[] { new SqlParameter("@FitemID", strFitemId), new SqlParameter("@FItemClassID", strFItemClassID) });
                    string strParentFNumber = string.Join(".", arrFnumber, 0, arrFnumber.Length - 1);
                    if (strParentFNumber.Length == 0 || CvtFunc.ToLeftString(strOldFNumber, strParentFNumber.Length + 1).Equals(strParentFNumber + "."))
                    {
                        arrFnumber = strOldFNumber.Split('.');
                    }
                    Update_t_Item(dbDestK3Con, strFitemId, string.Join(".", arrFnumber, 0, arrFnumber.Length), arrFName[arrFName.Length-1]);
                }
                else
                {
                    strFitemId = GetFParentID(strFItemClassID, dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, arrFnumber, arrFName, arrKeyValue);

                    base.SaveKeyMap(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strKeyValue, strFitemId);
                }
            
            WriteK3Log(dbDestK3Con, CvtFunc.ToInt(strFitemId));

            return CvtFunc.ToInt(strFitemId);
            

        }


        protected override void WriteK3Log(DBAccess dbDestK3Con, int intKeyValue)
        {
            string strSql = @"insert into mLog(logtype,keyvalue,logstatus)
                values(@logtype,@keyvalue,@logstatus)";
            dbDestK3Con.ExecuteSql(strSql, dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { 
                 {"logtype",strLogType}
                 ,{"keyvalue",intKeyValue.ToString()}
                 ,{"logstatus","1"}
             }).Value);

        }
        protected virtual void GetFNumber(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue, out string[] arrFnumber, out string[] arrFName, out string[] arrKeyValue)
        {
            throw new NotImplementedException();
                  
        }

        protected string GetFShortNumber(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strOrgKeyValue)
        {
            int intDestKeyValue = this.Update(dbOrgWebReadCon,dbOrgWebWriteCon,dbDestK3Con,strOrgKeyValue);

            return dbDestK3Con.GetValue("select fshortnumber from t_item where FitemID=@fitemno",new SqlParameter[]{new SqlParameter("@fitemno",intDestKeyValue.ToString())});
        }





        private string GetFParentID(string strFItemClassID, DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string[] arrFnumber, string[] arrFName, string[] arrKeyValue)
        {

            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();
            for (int i = 0; i < arrFnumber.Length; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FitemID
                strFItemId = dal.GetFItemId(dbDestK3Con, strFItemClassID, i + 1, strFParentID, string.Join(".", arrFnumber, 0, i + 1), arrFName[i], intFDetail);
                
                UpdateAccess(dbDestK3Con, strFItemId, strFParentID);
                



            }

            return    strFItemId ;
        }

        private void UpdateAccess(DBAccess dbDestK3Con, string strFItemId, string strFParentID)
        {
            string strSql =string.Format( @"
                if not exists(select * from Access_{0} where FitemID=@FitemID)
                begin
                 Insert into Access_{0}(FitemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FitemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))

                end
                ", strK3TableName);
            dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FitemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }
    }
}